/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.symbols;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.connect5.basics.ValueNode;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.constants.ViewConstants;
import org.jfonia.notation.Bar;

/**
 *
 * @author Rik Bauwens
 */
public class BarSymbol extends ComplexStaffSymbol
{
        private int barId;

        public BarSymbol(ValueNode<Double> frameX, ValueNode<Double> staffY,
                          MutableValueNode<Double> staffBase, MutableValueNode<Double> postStaffBase,
                          ValueNode<Double> xNode, MutableValueNode<Double> widthNode,
                          Bar bar)
        {
                super(frameX, staffY, staffBase, postStaffBase, xNode, widthNode,
                        new BasicValueNode<Integer>(ViewConstants.STAFF_LINE_COUNT - 1));
                init(bar.getBarTypeNode());
        }
        
        private void init(MutableValueNode<Integer> barId)
        {
                setBarId(barId.getValue());
              
                //Add observer
                barId.addObserver(new Observer()
                {
                        public void onNotify(Object source)
                        {
                                int barId = ((MutableValueNode<Integer>) source).getValue();
                                setBarId(barId);
                        }
                });
        }
        
        private void setBarId(int barId)
        {
                this.barId = barId;
                
                //Determine bounds
                double width = SymbolConstants.THIN_BAR_WIDTH;
                double height = ViewConstants.STAFF_LINE_SPACING *
                               (ViewConstants.STAFF_LINE_COUNT - 1);
                if(barId == SymbolConstants.DOUBLE_BAR)
                        width += (SymbolConstants.BAR_SPACING
                                + SymbolConstants.THICK_BAR_WIDTH);
                rectangle = new Rectangle2D.Double(getX(), Math.ceil(getY() - height / 2), width, height);
                
                //Update nodes
                updateNodes();
        }

        //Prevent that the rank can be changed (fixed)
        @Override
        public void setRank(int rank) {}

        @Override
        public void paintSymbol(Graphics2D g)
        {
                super.paintSymbol(g);
                double height = ViewConstants.STAFF_LINE_SPACING * (ViewConstants.STAFF_LINE_COUNT - 1);
        	g.setStroke(new BasicStroke(SymbolConstants.THIN_BAR_WIDTH));
        	g.draw(new Line2D.Double(getX(), Math.ceil(getY() - height / 2), getX(), getY() + height / 2));
                if(barId == SymbolConstants.DOUBLE_BAR)
                {
                        g.setStroke(new BasicStroke(SymbolConstants.THICK_BAR_WIDTH));
                        g.draw(new Line2D.Double(getX() + SymbolConstants.BAR_SPACING, Math.ceil(getY() - height / 2),
                                                 getX() + SymbolConstants.BAR_SPACING, getY() + height / 2));
                }
        }

}
